package com.gitee.zycra.union.api.dao;

import com.gitee.zycra.jdbc.common.BaseDao;
import com.gitee.zycra.jdbc.common.CommonSelector;
import com.gitee.zycra.union.api.common.model.ApiInterfaceCase;
import com.gitee.zycra.union.api.common.vo.InterfaceCaseVO;
import com.gitee.zycra.union.api.common.vo.MockInterfaceCaseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author zycra
 */
@Repository
public class ApiInterfaceCaseDao extends BaseDao<ApiInterfaceCase> {

    @Autowired
    private CommonSelector commonSelector;

    /**
     * 添加接口期望
     *
     * @param apiInterfaceCase 接口期望对象
     * @return 影响行数
     */
    public int insert(ApiInterfaceCase apiInterfaceCase) {
        return insertIntoTable(apiInterfaceCase);
    }

    /**
     * 更新接口期望
     *
     * @param apiInterfaceCase 接口期望对象
     * @return 影响行数
     */
    public int updateById(ApiInterfaceCase apiInterfaceCase) {
        return updateTableById(apiInterfaceCase);
    }

    /**
     * 删除接口期望
     *
     * @param id 接口期望ID
     * @return 影响行数
     */
    public int updateToDelete(Long id) {
        return updateTableById(new ApiInterfaceCase().setId(id).setDeleted(true));
    }

    /**
     * 查询接口期望
     *
     * @param interfaceId 接口ID
     * @return 查询结果
     */
    public List<InterfaceCaseVO> selectByInterfaceId(Long interfaceId) {
        return commonSelector.selectByParam(getJdbcTemplate(),
                "SELECT id, case_name, mock_value FROM api_interface_case WHERE interface_id = ? AND deleted = 0",
                InterfaceCaseVO.class, List.of(interfaceId));
    }

    /**
     * 查询接口期望
     *
     * @param interfaceId 接口ID
     * @return 查询结果
     */
    public List<MockInterfaceCaseVO> selectByInterfaceIdForAddInstance(Long interfaceId) {
        return commonSelector.selectByParam(getJdbcTemplate(),
                "SELECT id, case_name FROM api_interface_case WHERE interface_id = ? AND deleted = 0",
                MockInterfaceCaseVO.class, List.of(interfaceId));
    }
}
